﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ecommerce.Areas.Manager.Models;
using Ecommerce.Common;

namespace Ecommerce.Areas.Manager.Controllers
{
    [EAuthorization(LoginPage = EConst.ADMIN_LOGIN_PATH)]
    public class CategoryController : Controller
    {

        DatabaseContext db = new DatabaseContext();

        //
        // GET: /Category/

        public ActionResult Index()
        {
            return View(db.Categories.OrderByDescending(x => x.Order).ToList());
        }

        public ActionResult Regist()
        {
            Category c = new Category();
            return View(c);
        }

        [HttpPost]
        public ActionResult Regist(Category category)
        {
            if (ModelState.IsValid)
            {
                // check exist category
                if (db.Categories.Where(x => x.CategoryName == category.CategoryName).Count() != 0)
                {
                    ViewBag.ErrorMsg = "Category name is exist";
                    return View(category);
                }
                else
                {
                    category.LastUpdate = DateTime.Now;
                    category.Products = 0;
                    db.Categories.Add(category);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            return View(category);
        }

        public ActionResult Edit(string id)
        {
            // get parameter
            int categoryID = 0;
            try
            {
               categoryID = Int32.Parse(id);
            }
            catch
            {
                return RedirectToAction("Index");
            }

            // get category info
            Category c = db.Categories.Find(categoryID);
            if (c == null)
            {
                return RedirectToAction("Index");
            }

            return View(c);
        }

        [HttpPost]
        public ActionResult Edit(Category c)
        {
            if (ModelState.IsValid)
            {
                db.Entry(c).State = System.Data.EntityState.Modified;
                db.SaveChanges();

                return RedirectToAction("Index");
            }

            return View(c);
        }

        public ActionResult Delete(string id)
        {
            // get parameter
            int categoryID = 0;
            try
            {
                categoryID = Int32.Parse(id);
            }
            catch
            {
                return RedirectToAction("Index");
            }

            Category c = db.Categories.Find(categoryID);

            if (c.Products > 0)
            {
                ViewBag.ErrorMsg = "Can not delete this category because it have product.";
            }
            else
            {
                db.Categories.Remove(c);
                db.SaveChanges();
            }
            return View("Index", db.Categories.OrderByDescending(x => x.Order).ToList());
        }

    }
}
